package com.jaen.jaenandroidcalculator;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.addListenerToAllCalculatorButtons();
	}

	private void addListenerToAllCalculatorButtons(){
    	((Button) findViewById(R.id.button0)).setOnClickListener(this);
    	((Button) findViewById(R.id.button1)).setOnClickListener(this);
    	((Button) findViewById(R.id.button2)).setOnClickListener(this);
    	((Button) findViewById(R.id.button3)).setOnClickListener(this);
    	((Button) findViewById(R.id.button4)).setOnClickListener(this);
    	((Button) findViewById(R.id.button5)).setOnClickListener(this);
    	((Button) findViewById(R.id.button6)).setOnClickListener(this);
    	((Button) findViewById(R.id.button7)).setOnClickListener(this);
    	((Button) findViewById(R.id.button8)).setOnClickListener(this);
    	((Button) findViewById(R.id.button9)).setOnClickListener(this);
    	((Button) findViewById(R.id.buttonSum)).setOnClickListener(this);
    	((Button) findViewById(R.id.buttonSubtract)).setOnClickListener(this);
    	((Button) findViewById(R.id.buttonMultiply)).setOnClickListener(this);
    	((Button) findViewById(R.id.buttonDivide)).setOnClickListener(this);
    	((Button) findViewById(R.id.buttonEqual)).setOnClickListener(this);
    	((Button) findViewById(R.id.buttonPoint)).setOnClickListener(this);
    	((ImageButton) findViewById(R.id.deleteButton)).setOnClickListener(this);
	}
	
	@Override
	public void onClick(View view) {
		TextView tv = (TextView) findViewById(R.id.operationsTextView);
			switch(view.getId()){
			case R.id.button0:
				tv.setText(tv.getText().toString() + "0");
				break;
			case R.id.button1:
				tv.setText(tv.getText().toString() + "1");
				break;
			case R.id.button2:
				tv.setText(tv.getText().toString() + "2");
				break;
			case R.id.button3:
				tv.setText(tv.getText().toString() + "3");
				break;
			case R.id.button4:
				tv.setText(tv.getText().toString() + "4");
				break;
			case R.id.button5:
				tv.setText(tv.getText().toString() + "5");
				break;
			case R.id.button6:
				tv.setText(tv.getText().toString() + "6");
				break;
			case R.id.button7:
				tv.setText(tv.getText().toString() + "7");
				break;
			case R.id.button8:
				tv.setText(tv.getText().toString() + "8");
				break;
			case R.id.button9:
				tv.setText(tv.getText().toString() + "9");
				break;
			case R.id.buttonPoint:
				tv.setText(tv.getText().toString() + ".");
				break;
			case R.id.buttonSum:
				tv.setText(tv.getText().toString() + "+");
				break;
			case R.id.buttonSubtract:
				tv.setText(tv.getText().toString() + "-");
				break;
			case R.id.buttonMultiply:
				tv.setText(tv.getText().toString() + "*");
				break;
			case R.id.buttonDivide:
				tv.setText(tv.getText().toString() + "/");
				break;
			case R.id.buttonEqual:
				tv.setText(this.calculateOperation(tv.getText().toString()));
				break;
			case R.id.deleteButton:
				tv.setText(this.deleteCharacter(tv.getText().toString()));
				break;
			}
	}
	
	/**
	 * @param String mathematicalExpression
	 * @return String
	 */
	private String calculateOperation(String mathematicalExpression){
		return "Some result is expected here.";
	}
	
	/**
	 * @param String mathematicalExpression
	 * @return String
	 */
	private String deleteCharacter(String mathematicalExpression){
		if(!mathematicalExpression.isEmpty()){
			return mathematicalExpression.substring(0,mathematicalExpression.length()-1);	
		}
		return "";
	}
	
}
